import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_model/acg/comic_chapters_model.dart';
import 'package:xvideos/hj_model/cartoon_media_info.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';

import 'acg_source_manager.dart';

//漫画子集底部弹窗
class CartoonSubSetAlert extends StatefulWidget {
  final ACGSourceManager? sourceManager;

  const CartoonSubSetAlert({super.key, this.sourceManager});

  @override
  State<CartoonSubSetAlert> createState() => _CartoonSubSetAlertState();
}

class _CartoonSubSetAlertState extends State<CartoonSubSetAlert> {
  CartoonMediaInfo? get mediaInfo => widget.sourceManager?.mediaInfo;
  List<ComicChapterInfo> allEpisodes = [];
  bool sortType = true; // true-升序  false-降序

  @override
  void initState() {
    super.initState();
    allEpisodes.addAll(widget.sourceManager?.allEpisodes ?? []);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: Get.width,
        height: 425,
        decoration: BoxDecoration(
          color: Color(0xff030F18),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20),
            topRight: Radius.circular(20),
          ),
        ),
        child: Column(
          children: [
            18.sizeBoxH,
            Container(
              width: 36,
              height: 5,
              decoration: BoxDecoration(
                color: Color(0xff989898),
                borderRadius: BorderRadius.circular(100),
              ),
            ),
            18.sizeBoxH,
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                16.sizeBoxW,
                Text(
                  '漫画列表',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                10.sizeBoxW,
                Spacer(),
                InkWell(
                  onTap: () {
                    changeSortType();
                  },
                  child: Row(
                    children: [
                      Image.asset(
                        'acg_sort.png'.acgImgPath,
                        width: 20,
                      ),
                      4.sizeBoxW,
                      Text(
                        sortType ? '正序' : '倒序',
                        style: TextStyle(
                          color: Colors.white.withOpacity(.5),
                          fontSize: 15,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ],
                  ),
                ),
                22.sizeBoxW,
              ],
            ),
            Expanded(
              child: _buildScaleWidget(),
            )
          ],
        ));
  }

  //
  _buildScaleWidget() {
    return ListView.builder(
      padding: EdgeInsets.symmetric(vertical: 9),
      itemCount: allEpisodes.length,
      itemBuilder: (context, index) {
        ComicChapterInfo? info = allEpisodes[index];
        int realIndex = (info.episodeNumber ?? 1) - 1;
        bool sel = realIndex == widget.sourceManager?.index;
        return InkWell(
          onTap: () => Get.back(result: realIndex),
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 16, vertical: 9),
            child: Text(
              '第${info.episodeNumber ?? 0}${widget.sourceManager?.mediaInfo?.unit ?? ''} ${info.name ?? ''}',
              style: TextStyle(
                color: sel ? Colors.white.withOpacity(.9) : Colors.white.withOpacity(.45),
                fontSize: 16,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
        );
      },
    );
  }

  changeSortType() {
    sortType = !sortType;
    allEpisodes.clear();
    if (sortType) {
      allEpisodes.addAll((widget.sourceManager?.allEpisodes ?? []));
    } else {
      allEpisodes.addAll((widget.sourceManager?.allEpisodes ?? []).reversed);
    }
    setState(() {});
  }
}
